import 'package:audioplayers/audioplayers.dart';
import 'package:flutter/material.dart';
import 'package:kuwo_music/pages/components/roundPainter.dart';
import 'package:kuwo_music/provider/module/common.dart';
import 'package:provider/provider.dart';

class RotateSong extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _RotateSong();
}

class _RotateSong extends State<RotateSong> with TickerProviderStateMixin {
  AnimationController _rotationController;
  // AnimationController _percentController;
  // Animation _animation;

  @override
  void initState() {
    _rotationController =
        AnimationController(duration: Duration(seconds: 20), vsync: this);

    _rotationController.addStatusListener((AnimationStatus status) {
      if (status == AnimationStatus.completed) {
        _rotationController.reset();
        _rotationController.forward();
      }
    });

    //开始动画
    // _rotationController.forward();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    CommonInfo _commonInfo = context.watch<CommonInfo>();
    // print(_commonInfo.audioPercent);
    return Container(
      width: 40,
      height: 40,
      child: CustomPaint(
          painter: RoundPainter(
              width: 5,
              lineColor: Colors.white,
              completeColor: Colors.red,
              completePercent: _commonInfo.audioPercent),
          child: Builder(builder: (BuildContext context) {
            switch (_commonInfo.audioPlayerState) {
              case AudioPlayerState.PAUSED:
                {
                  _rotationController.stop();
                  return PausedSing(rotationController: _rotationController);
                }
              case AudioPlayerState.PLAYING:
                {
                  _rotationController.forward();
                  return Singing(rotationController: _rotationController);
                }
              case AudioPlayerState.STOPPED:
                {
                  _rotationController.stop();
                  return PausedSing(rotationController: _rotationController);
                }
              default:
                {
                  // _rotationController.forward();
                  _rotationController.stop();
                  return PausedSing(rotationController: _rotationController);
                }
            }
          })),
    );
  }

  @override
  void dispose() {
    _rotationController.dispose();
    super.dispose();
  }
}

class Singing extends StatelessWidget {
  const Singing({
    Key key,
    @required AnimationController rotationController,
  })  : _rotationController = rotationController,
        super(key: key);

  final AnimationController _rotationController;

  @override
  Widget build(BuildContext context) {
    return RotationTransition(
      turns: _rotationController,
      child: Container(
        height: 40,
        width: 40,
        child: CircleAvatar(
          backgroundImage: AssetImage('static/images/404.jpg'),
        ),
      ),
    );
  }
}

class PausedSing extends StatelessWidget {
  const PausedSing({
    Key key,
    @required AnimationController rotationController,
  })  : _rotationController = rotationController,
        super(key: key);

  final AnimationController _rotationController;

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        RotationTransition(
          turns: _rotationController,
          child: Container(
            height: 40,
            width: 40,
            child: CircleAvatar(
              backgroundImage: AssetImage('static/images/404.jpg'),
            ),
          ),
        ),
        Align(
          child: Icon(
            Icons.play_arrow,
            color: Colors.white38,
          ),
        )
      ],
    );
  }
}
